import pandas as pd


test_df = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/test_format1.csv')
train_df = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/train_format1.csv')
user_info = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/user_info_format1.csv')
user_log = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/user_log_format1.csv').rename(columns={'seller_id':'merchant_id'})


#填充缺失值
# user_info['gender'].fillna(2, inplace=True) #2和null都代表性别不确定
# user_info.fillna({'age_range': -1}, inplace=True)
# user_log.fillna({'brand_id': -1}, inplace=True)

import pandas as pd
# 假设 user_info 是一个 DataFrame
user_info = pd.DataFrame({
    'gender': [1, None, 0],
    'age_range': [2, None, 3]
})
# 正确的填充缺失值方式
user_info = user_info.fillna({'gender': 2, 'age_range': -1})

# 查看变量分布
# action type中大部分都是click，真正的购买行为很少
from matplotlib import pyplot as plt
plt.figure(figsize=(12,4))
plt.subplot(1,3,1)
plt.hist(user_log['time_stamp']) #time_tamp 购买时间（格式：mmdd）
plt.title('time_stamp')
plt.subplot(1,3,2)
plt.hist(user_log['action_type'])
plt.title('action_type') #action_type包含{0, 1, 2, 3}，0表示单击，1表示添加到购物车，2表示购买，3表示添加到收藏夹
plt.subplot(1,3,3)
plt.hist(user_info['gender']) #gender用户性别。0表示女性，1表示男性，2和NULL表示未知
plt.title('gender')
plt.show()

# 各种行为基本上都是在双十一激增，这一块的特殊性是有的挖的
from matplotlib import pyplot as plt
plt.plot(user_log[user_log['action_type']==2].groupby('time_stamp').count()['action_type'])
plt.show()

# 特征构造
# 聚合特征

seller_group = user_log.groupby(["merchant_id","action_type"]).count()[["user_id"]].reset_index().rename(columns={'user_id':'count'})
import gc
del user_log
gc.collect()


# seller_group = user_log.groupby(["seller_id","action_type"]).count()[["user_id"]].reset_index().rename(columns={'user_id':'count'})

# 渗透率：
# 将 'seller_id' 修改为 'merchant_id'
seller_feature = seller_group[seller_group['action_type'] == 0][['merchant_id', 'count']].reset_index()[
    ['merchant_id', 'count']].rename(
    columns={'count': 'click_count'})
def _get_action_cnt(num):
    seller_df = seller_group[seller_group['action_type'] == num]
    cnt_list = []
    # 将 'seller_id' 修改为 'merchant_id'
    for i in seller_feature['merchant_id']:
        l = list(seller_df['count'][seller_df['merchant_id'] == i])
        if l:
            cnt_list.append(l[0])
        else:
            cnt_list.append(0)
    return cnt_list
seller_feature['cart_count'] = _get_action_cnt(1)
seller_feature['sell_count'] = _get_action_cnt(2)
seller_feature['star_count'] = _get_action_cnt(3)
print(seller_feature.head())


# 合并数据并去掉不存在的列名（注释掉删除 'seller_id' 的操作）
train_df = train_df.merge(seller_feature, on="merchant_id", how='left')
test_df = test_df.merge(seller_feature, on="merchant_id", how='left')
# 保存文件
train_df.to_csv('train_v1.csv', index=False)
test_df.to_csv('test_v1.csv', index=False)
# 重新读取文件
train_df = pd.read_csv('数据清洗/train_v1.csv')
test_df = pd.read_csv('./test_v1.csv')

train_df = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/train_format2.csv')
test_df = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/test_format2.csv')

双11相关特征

# 重新读取 user_log
user_log = pd.read_csv('D:/iddaima/zg5/tmall-repurchase-prediction/data/data_format1/user_log_format1.csv').rename(columns={'seller_id':'merchant_id'})
# 双11相关特征
for i in (0,1,3):
    user_group = user_log[(user_log['time_stamp']<1111) & (user_log['action_type']==i)].groupby(
        ["user_id","merchant_id"]).count()[["action_type"]].reset_index()
    train_df = train_df.merge(user_group, on=['user_id','merchant_id'],how='left').rename(
        columns={'action_type':'action_type{}'.format(i)})
    test_df = test_df.merge(user_group, on=['user_id','merchant_id'],how='left').rename(
        columns={'action_type':'action_type{}'.format(i)})
for i in (0, 1, 3):
    user_group = user_log[(user_log['time_stamp'] == 1111) & (user_log['action_type'] == i)].groupby(
        ["user_id", "merchant_id"]).count()[["action_type"]].reset_index()
    train_df = train_df.merge(user_group, on=['user_id', 'merchant_id'], how='left').rename(
        columns={'action_type': 'action_type{}_in1111'.format(i)})
    test_df = test_df.merge(user_group, on=['user_id', 'merchant_id'], how='left').rename(
        columns={'action_type': 'action_type{}_in1111'.format(i)})
train_df = train_df.fillna(0)
test_df = test_df.fillna(0)
for i in (0,1,3):
    train_df['action{}_rate'.format(i)] = train_df.apply(lambda x:x['action_type{}_in1111'.format(i)]/x['action_type{}'.format(i)] if x['action_type{}'.format(i)]>0 else -1, axis=1)
    test_df['action{}_rate'.format(i)] = test_df.apply(lambda x:x['action_type{}_in1111'.format(i)]/x['action_type{}'.format(i)] if x['action_type{}'.format(i)]>0 else -1, axis=1)
train_df.to_csv('./train_v2.csv')
test_df.to_csv('./test_v2.csv')

